package com.newproject.module;

import android.support.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.newproject.view.CircleView;

import java.util.Map;

import javax.annotation.Nonnull;


public abstract class BaseViewManager<T extends CircleView> extends SimpleViewManager<T> {
    public static final int COMMAND_SCROLL_TO = 1;

    /**
     * 创建UI组件实例
     */
    @Override
    protected T createViewInstance(ThemedReactContext reactContext) {
        CircleView circleView = new CircleView(reactContext);
        circleView.setOnClickListener(v -> {
            //            System.out.println("onCLick");
            WritableMap map = Arguments.createMap();
            map.putString("key", "value");
            sendEvent(reactContext, "EventReminder", map);
        });
        return (T) circleView;
    }

    /**
     * 传输背景色参数
     */
    @ReactProp(name = "color")
    public void setColor(T view, Integer color) {
        view.setColor(color);
    }

    /**
     * 传输半径参数
     */
    @ReactProp(name = "radius")
    public void setRadius(T view, Integer radius) {
        view.setRadius(radius);
    }

    @javax.annotation.Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        return MapBuilder.of(
                "commend0", COMMAND_SCROLL_TO
        );
    }

    @Override
    public void receiveCommand(@Nonnull T root, int commandId, @javax.annotation.Nullable ReadableArray args) {
        super.receiveCommand(root, commandId, args);

        if (commandId == COMMAND_SCROLL_TO) {
            System.out.println("receiveCommand: " + args.toString());
        }
    }

    private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
        reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit(eventName, params);
    }
}